package main

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"go-process/config"
	"go-process/email"
	"go-process/mylog"
	"go-process/router"
	"log/slog"
	"os"
)

func init() {
	fmt.Println("初始化配置文件")
	config.InitConfig()
}

func main() {

	// 记录不同级别的日志
	handler := &mylog.CustomHandler{Writer: os.Stdout}
	logger := slog.New(handler)

	logger.Info("自定义处理器", "custom", "field", "number", 42)

	//sendEmail()

	// 创建一个默认的路由引擎
	r := gin.Default()
	//task.Task.Start()
	//defer task.Task.Stop()
	router.ApiRouter(r)
	background := context.Background()

	buckets, err := config.MinIoClient.ListBuckets(background)
	if err != nil {
		slog.Error("获取bucket列表失败", "error", err)
	} else {
		for _, bucket := range buckets {
			slog.Info("bucket", "name", bucket.Name)
			if bucket.Name == "wyn" {
				//设置权限为公开访问
				//policy := `{"Version": "2012-10-17","Statement": [{"Action": ["s3:GetObject"],"Effect": "Allow","Principal": {"AWS": ["*"]},"Resource": ["arn:aws:s3:::wyn/*"],"Sid": ""}]}`
				policy := `{"Version": "2012-10-17","Statement": []}` //设置权限为私有
				config.MinIoClient.SetBucketPolicy(background, bucket.Name, policy)
			}
		}
	}

	// 启动 HTTP 服务，默认在 localhost:8080 启动服务
	r.Run()
}

func sendEmail() {
	template := email.EmailTemplate{
		Data: map[string]interface{}{
			"Name":    "Rise",
			"AppName": "Go-Process",
		},
		Subject: "欢迎使用 Go-Process",
		To:      "407473978@qq.com",
	}
	err := email.SendTemplateEmail(&template)
	if err != nil {
		fmt.Println("出错了:", err)
	}
	fmt.Println(err)
}
